<template>
	<view>
		<u-sticky v-if="globalMessages.length > 0" :top='0'>
			<u-notice-bar direction="column" :text="globalMessages"></u-notice-bar>
			<!-- <u-notify ref="globalNotify" message="globalMessage"></u-notify> -->
		</u-sticky>
	</view>
</template>

<script>
	import envParams from '@/api/env.js'
	import imSupport from '@/utils/imSupport';
	export default {
		name: "global-notify",
		props: {
			groupId: {
				require: false,
				default: ''
			}
		},
		data() {
			return {
				show: false,
				globalMessages: [],
				globalMessage: '收到新的消息',
				newMsgMap: {},
			};
		},
		methods: {
			receiveMsg: async function(item) {
				console.log('global receiveMsg', item)
				const pages = getCurrentPages()
				// 当前页面路径
				const pageRoute = pages[pages.length - 1].route
				let systemRoles = await this.$commonData.getSystemRoles()
				let isOnline = this.$commonData.lawyerStatus('IM')
				let isAdmin = this.$commonData.isAdmin()
				let isLawyer = systemRoles.indexOf('LAWYER_IM') >= 0;
				let isLawyerM = systemRoles.indexOf('LAWYER_MANAGER') >= 0;
				let extra = item.content ? item.content.extra : 'auto'
				let imUserId = this.$commonData.imUserId()
				switch (extra) {
					case 'create':
						console.log(isOnline, isLawyer)
						// 若律师非上线的 不收到提醒 admin都可以
						if ((isOnline && isLawyer) || isAdmin) {
							// 系统收到新的咨询件
							uni.$emit('refreshIM')
							this.showMessage('系统收到新的咨询件！', item)
						}
						break
					case 'access':
						if (item.targetId == envParams.VUE_APP_IM_SYSTEM_USER_PRE + 'SYSTEM_IM_GROUP') {
							// 超级群的咨询件被消化通知
							uni.$emit('refreshIM')
						} else {
							//律师接入
							imSupport.noticeIM('access', item)
							if (pageRoute == 'pages/consultant/consultant') {
								// 列表页面刷新
								uni.$emit('reloadList')
							}
							this.showMessage('您有一件新的咨询件待回复！', item)
							// if (pageRoute != 'pages/consultant/consultant' && pageRoute != 'pages/consultant/consultantIM') {
							// 	toast('您有一件新的咨询件待回复')
							// }
						}
						break
					case 'destroy':
						//咨询件被销毁
						uni.$emit('refreshIM')
						break
					case 'finish':
						//结束 todo
						imSupport.noticeIM('finish')
						if (pageRoute == 'pages/consultant/consultantIM') {
							if (this.groupId != item.targetId) {
								this.showMessage('您有一件咨询件已结束咨询，该聊天室将于3秒后主动关闭！', item)
							} else {
								uni.$emit('chartMsg', item)
							}
						} else {
							if (pageRoute == 'pages/consultant/consultant') {
								uni.$emit('reloadList')
							}
							this.showMessage('您有一件咨询件已结束咨询，聊天室将于3秒后主动关闭！', item)
						}
						break;
					case 'interpose':
						// 表示有律师介入咨询件 或者被指派介入咨询 
						//当前在进行中
						if (pageRoute == 'pages/consultant/consultant') {
							uni.$emit('refreshIM')
						}
						uni.$emit('chartMsg', item)
						break
					default:
						if (item.senderUserId != imUserId) {
							let msgs = this.newMsgMap[item.targetId] || 0
							msgs++
							this.$set(this.newMsgMap, item.targetId, msgs)
							uni.setStorageSync('newMsgMap', this.newMsgMap)
							imSupport.noticeIM('message')
						}
						switch (pageRoute) {
							case 'pages/consultant/consultant':
								if (item.senderUserId != imUserId) {
									uni.$emit('listMsg', item)
									this.showMessage('您收到一条新消息，请及时回复！', item)
								}
								break
							case 'pages/consultant/consultantIM':
								console.log('this.groupId', this.groupId)
								if (this.groupId != item.targetId) {
									if (item.senderUserId != imUserId) {
										this.showMessage('其它咨询件收到新消息，请及时回复！', item)
									}
								} else {
									uni.$emit('chartMsg', item)
								}
								break
							default:
								if (item.senderUserId != imUserId) {
									this.showMessage('您收到一条新消息，请及时回复！', item)
								}
								break
						}

						break
				}
			},
			showMessage: function(message, item) {
				// this.show = true
				let that = this
				let id = item.messageUId
				this.globalMessages.push(message)
				setTimeout(() => {
					if (this.globalMessages.length == 1) {
						this.globalMessages = []
					} else {
						this.globalMessages = this.globalMessages.splice(1)
					}
				}, 5000)
			}
		},
		beforeMount() {
			// size: 100,
			let _this = this
			this.newMsgMap = uni.getStorageSync('newMsgMap') || {}
			// 先移除监听
			uni.$off('receiveMsg')
			uni.$on('receiveMsg', function(message) {
				console.log('globalNotify receiveMsg ，携带参数为：' + message);
				_this.receiveMsg(message)
			})
		}
	}
</script>
<style>

</style>